package cn.caijiapeng.spt.bean;

import java.util.HashMap;


public class Resp extends HashMap<String, Object>{
	private static final long serialVersionUID = 5537563383782168980L;
	public enum Code{
		OK(0),
		FAIL(5),
		LOGIN_FAIL(101),
		ACCESS_TOKEN_INVALID(401);
		
		
		public final Integer code;
		Code(Integer code){
			this.code=code;
		}
	}
	
	
	public static Resp loginFail(String msg) {
		return new Resp(Code.LOGIN_FAIL, msg, null);
	}
	
	public static Resp ok(Object data) {
		return new Resp(Code.OK, "操作成功", data);
	}
	
	public static Resp ok(String msg, Object data) {
		return new Resp(Code.OK, msg, data);
	}
	
	public static Resp ok() {
		return new Resp(Code.OK, "操作成功", null);
	}
	
	public static Resp okMessage(String msg) {
		return new Resp(Code.OK, msg, null);
	}
	
	public static Resp fail() {
		return new Resp(Code.FAIL, "操作失败", null);
	}
	
	public static Resp fail(String message) {
		return new Resp(Code.FAIL, message, null);
	}
	
	public static Resp by(Code code, String message) {
		return new Resp(code, message, null);
	}
	
	public static Resp by(Code code, String message, Object data) {
		return new Resp(code, message, data);
	}
	
	public Resp set(String key, Object value) {
		put(key, value);
		return this;
	}
	
	public Resp delete(String key) {
		super.remove(key);
		return this;
	}
	
	private Resp(Code code, String error,Object data) {
		put("code", code.code);
		put("message", error);
		if(data != null) {
			put("data", data);
		}
	}
	
	private Resp(Object data) {
		put("code", Code.OK.code);
		put("message", "");
		if(data != null) {
			put("data", data);
		}
	}
}
